//
//  TRMsgViewController.m
//  WeiMiLan
//
//  Created by Mac on 15/10/29.
//  Copyright © 2015年 Mac. All rights reserved.
//

#import "TRMsgViewController.h"
#import "TRMsgCell.h"

@interface TRMsgViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) UITableView *tabelVoew;
@property (strong, nonatomic) NSMutableArray *array;
@end

@implementation TRMsgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigation];
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.y = 68;
    [self.view addSubview:tableView];
    self.tabelVoew = tableView;
    self.tabelVoew.delegate = self;
    self.tabelVoew.dataSource = self;
    self.tabelVoew.rowHeight = 66;
    self.tabelVoew.tableFooterView = [[UIView alloc]init];
    
    UINib *nib = [UINib nibWithNibName:@"TRMsgCell" bundle:nil];
    [self.tabelVoew registerNib:nib forCellReuseIdentifier:@"resuableCell"];
    
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.array = ApplicationDelegate.messageArr;
    self.array = [NSMutableArray bq_arrayWithSortedOfDict:self.array WithSpecifiedKey:@"time" isAscending:NO];
}
- (void)initNavigation
{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.weChatNavigationBar=[[WeChatNavigationBar alloc] init];
    [self.view addSubview:self.weChatNavigationBar];
    
    self.weChatNavigationBar.titleLabel.text=@"事件提醒";
    [self.weChatNavigationBar.leftButton addTarget:self action:@selector(exitHome) forControlEvents:UIControlEventTouchUpInside];
    self.weChatNavigationBar.rightButton.hidden = YES;
}
-(void)exitHome
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = self.array.count;
    return count == 0 ? 0 : count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *IDs = @"resuableCell";
    TRMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:IDs];
    if (cell == nil) {
        cell = [[TRMsgCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:IDs];
    }
    if (self.array.count == 0) {
        return nil;
    }
    NSDictionary *dict = self.array[indexPath.row];
    cell.tipLabel.text = [NSString stringWithFormat:@"消息%zd :",indexPath.row + 1];
    cell.msgLabel.text = dict[@"message"];
    cell.timeLabel.text = dict[@"time"];
    
    cell.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = self.array[indexPath.row];
    if (dict[@"messageID"] != nil) {
        TRProductDetailViewController *vc = [[TRProductDetailViewController alloc]init];
        vc.productTypeId = dict[@"messageID"];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
@end
